package office.word.docx4j;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FopFactoryBuilder;
import org.docx4j.Docx4J;
import org.docx4j.XmlUtils;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.convert.out.fo.renderers.FORendererApacheFOP;
import org.docx4j.convert.out.fopconf.Fop;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.fonts.fop.fonts.FontEventAdapter;
import org.docx4j.fonts.fop.fonts.autodetect.FontFileFinder;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.org.apache.fop.events.DefaultEventBroadcaster;

import java.io.File;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import org.docx4j.model.table.TableModelCell;

/**
 * 参考：
 *  - W3C XSL 协议：https://www.w3.org/TR/xsl/#rend-border
 *  - 使用FOP在生成的PDF中保留空格：https://qa.1r1g.com/sf/ask/828051311/
 *  - ValidationException：fo:table-row 缺少子元素：https://issues.apache.org/jira/browse/FOP-2968
 * @author: zhanglu
 * @date: 2023/7/10 10:25
 * @modify: 修改人信息, 修改内容 2023/7/10 10:25
 */
public class WordToPdfTest {

    public static void main(String[] args)
            throws Exception {
        String inputfilepath = "/Users/zhanglu/Desktop/tmp/docx4j/word_template2.docx";
        String outputfilepath = "/Users/zhanglu/Desktop/tmp/docx4j/word_template2.pdf";
        FontFileFinder fontFileFinder = new FontFileFinder(new FontEventAdapter(new DefaultEventBroadcaster()));
        List<URL> fontFileList = fontFileFinder.find("/Users/zhanglu/Desktop/tmp/Fonts");
        fontFileList.forEach(o -> {
            try {
                PhysicalFonts.addPhysicalFont(o.toURI());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
        Mapper fontMapper = new IdentityPlusMapper();
        wordMLPackage.setFontMapper(fontMapper);
        fontMapper.put("方正仿宋", PhysicalFonts.get("STFangSong"));
        fontMapper.put("方正仿宋_GBK", PhysicalFonts.get("STFangSong"));
        fontMapper.put("STFangSong", PhysicalFonts.get("STFangSong"));
        fontMapper.put("Calibri", PhysicalFonts.get("Calibri"));
        fontMapper.put("Symbol", PhysicalFonts.get("Symbol"));
        fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
        fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
        fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
        fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
        fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
        fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
        fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
        fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
        fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
        fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
        fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
        fontMapper.put("等线", PhysicalFonts.get("SimSun"));
        fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
        fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
        fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
        fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
        fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
        fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
        fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
        fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
        fontMapper.put("宋体扩展",PhysicalFonts.get("simsun-extB"));
        fontMapper.put("仿宋_GB2312",PhysicalFonts.get("FangSong_GB2312"));
        fontMapper.put("新細明體",PhysicalFonts.get("SimSun"));
        OutputStream os = new java.io.FileOutputStream(outputfilepath);
//        Docx4J.toPDF(wordMLPackage, os);


        FOSettings foSettings = new FOSettings(wordMLPackage);
        System.out.println(XmlUtils.marshaltoString(foSettings.getFopConfig(), Context.getFopConfigContext()));
        FopFactoryBuilder fopFactoryBuilder = FORendererApacheFOP.getFopFactoryBuilder(foSettings) ;
        FopFactory fopFactory = fopFactoryBuilder.build();
        FOUserAgent foUserAgent = FORendererApacheFOP.getFOUserAgent(foSettings, fopFactory);
        foUserAgent.setTitle("my title");
        File fo = new File("/Users/zhanglu/Desktop/tmp/docx4j/word_template2.fo");
        foSettings.setFoDumpFile(fo);
        Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

//
//        List<String> lines = FileUtil.readUtf8Lines("/Users/zhanglu/Desktop/tmp/docx4j/word_template2.fo");
//        for (int i = 0; i < lines.size(); i++) {
//            if(i != 2){
//                continue;
//            }
//            String line = lines.get(i);
//            String sub = StrUtil.sub(line, 410, 440);
//            System.out.println(sub);
//        }

    }

}
